//
//  WBVisitorView.swift
//  Swift_sina
//
//  Created by TFB_WGP on 2017/8/21.
//  Copyright © 2017年 TFB_WGP. All rights reserved.
//

import UIKit

class WBVisitorView: UIView {
    /// 访客使徒信息
    var visitorInfo: [String:String]?{
        didSet{
            setupInfo(dict: visitorInfo ?? [:])
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: - 设置访客视图信息
    /// 通过字典设置方可视图的信息
    func setupInfo(dict: [String:String]) -> Void {
        
        guard let imageNmae = dict["imageName"],
            let message = dict["message"] else{
                return
        }
        
        tipLabel.text = message
        if imageNmae == "" {
            startAnimation()
            registerBtn.isHidden = true
            loginBtn.isHidden = true
            return
        }
        
        iconView.image = UIImage(named: imageNmae)
        //其他控制器的房子是徒步需要显示
        houseIconView.isHidden = true
        maskIconView.isHidden = true
        findBtn.isHidden = true
    }
    
//    MARK: - 私有控件
    ///头像视图
    fileprivate lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    fileprivate lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_mask"))
    /// 房子
    fileprivate lazy var houseIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    /// 标签label
    fileprivate lazy var tipLabel: UILabel = {
        let label = UILabel.cz_label(withText: "", fontSize: tipFont, color: UIColor.lightGray)
        label?.textAlignment = .center
        return label!
    }()
//    fileprivate lazy var registerBtn: UIButton = UIButton.cz_textButton("注册", fontSize: btnFont, normalColor: UIColor.darkGray, color: UIColor.orange, controlState: .highlighted)
    /// 注册按钮
    lazy var registerBtn: UIButton = {
        
        let btn = UIButton.cz_textButton("注册", fontSize: btnFont, normalColor: UIColor.orange, normalImage: "common_button_alpha", highlightImage: "common_button_alpha_highlighted")
        return btn!
    }()
    
    /// 登录按钮
    lazy var loginBtn: UIButton = {
        
        let btn = UIButton.cz_textButton("登录", fontSize: btnFont, normalColor: UIColor.darkGray, normalImage: "common_button_alpha", highlightImage: "common_button_alpha_highlighted")
        return btn!
    }()
    
    /// 找人按钮
     lazy var findBtn: UIButton = {
        
        let btn = UIButton.cz_textButton("找感兴趣的人", fontSize: btnFont, normalColor: UIColor.orange, normalImage: "common_button_alpha", highlightImage: "common_button_alpha_highlighted")
        return btn!
    }()
}

extension WBVisitorView{
    func setupUI() -> Void {
        
        backgroundColor = UIColor.cz_color(withRed: 237, green: 237, blue: 237)
        
//        1.添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(houseIconView)
        addSubview(tipLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        addSubview(findBtn)
        
//        2.布局
        iconView.snp.makeConstraints { (ConstraintMaker) in
            ConstraintMaker.centerX.equalTo(self)
            ConstraintMaker.centerY.equalTo(self).offset(-60)
        }
        maskIconView.snp.makeConstraints { (ConstraintMaker) in
            ConstraintMaker.top.left.right.equalTo(self)
            ConstraintMaker.bottom.equalTo(findBtn.snp.top)
        }
        houseIconView.snp.makeConstraints { (ConstraintMaker) in
            ConstraintMaker.centerX.equalTo(self)
            ConstraintMaker.centerY.equalTo(self).offset(-60)
        }
        tipLabel.snp.makeConstraints { (ConstraintMaker) in
            ConstraintMaker.centerX.equalTo(self)
            ConstraintMaker.top.equalTo(iconView.snp.bottom)
        }
        registerBtn.snp.makeConstraints { (ConstraintMaker) in
            ConstraintMaker.right.equalTo(self.snp.centerX).offset(-10)
            ConstraintMaker.top.equalTo(tipLabel.snp.bottom).offset(20)
            ConstraintMaker.width.equalTo(120)
        }
        loginBtn.snp.makeConstraints { (ConstraintMaker) in
            ConstraintMaker.left.equalTo(self.snp.centerX).offset(10)
            ConstraintMaker.width.centerY.equalTo(registerBtn)
        }
        findBtn.snp.makeConstraints { (ConstraintMaker) in
            ConstraintMaker.centerX.equalTo(self)
            ConstraintMaker.top.equalTo(tipLabel.snp.bottom).offset(20)
            ConstraintMaker.left.equalTo(tipLabel).offset(15)
            ConstraintMaker.right.equalTo(tipLabel).offset(-15)
        }
        
    }
    
    func startAnimation() -> Void {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * Double.pi
        anim.repeatCount = MAXFLOAT
        anim.duration = 15
        
        //如果iconView删除,动画会一起销毁
        //动画完成不删除
        anim.isRemovedOnCompletion = false
        iconView.layer.add(anim, forKey: nil)
    }
    
}
